package cn.tedu.blog.server.utils;

import cn.tedu.blog.server.exception.ServiceException;
import lombok.Data;

/**
 * @Author: 雷茂林
 * @Date:2022/7/6 14:44
 * @Description: 业务请求返回值封装工具类
 */
@Data
public class JsonResult {

    private Integer code;
    private String message;
    /**
     * 处理业务成功时，需要响应给客户端的数据
     */
    private Object data;


    public static JsonResult ok() {
        return ok(null);
    }


    public static JsonResult ok(Object data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = ServiceCode.OK;
        jsonResult.data = data;
        return jsonResult;
    }


    public static JsonResult fail(ServiceException e){
        return fail(e.getServiceCode(), e.getMessage());
    }



    public static JsonResult fail(Integer code, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.code = code;
        jsonResult.message = message;
        return jsonResult;
    }
}
